//index.js
//获取应用实例
import {
  mainInList,
  doctorList,
  hospitalList, categoryByLevel, sucessCaseList
} from '../../api/apis.js'
const app = getApp()
Page({
  data: {
    bannerConfig: {
      indicatorDots: false, //小点
      autoplay: true, //是否自动轮播
      interval: 5000, //间隔时间
      duration: 800 //滑动时间
    },
    menus: [
      {
        img: '../../assets/f.png',
        text: '医学美肤',
        url: '../project-list/project-list?did=32'
      },
      {
        img: '../../assets/d.png',
        text: '眼部',
        url: '../project-list/project-list?did=30'
      },
      {
        img: '../../assets/e.png',
        text: '鼻部',
        url: '../project-list/project-list?did=31'
      },
      {
        img: '../../assets/a.png',
        text: '注射',
        url: '../project-list/project-list?did=34'
      },
      {
        img: '../../assets/h.png',
        text: '胸部',
        url: '../project-list/project-list?did=36'
      },
      {
        img: '../../assets/j.png',
        text: '口腔',
        url: '../project-list/project-list?did=39'
      },
      {
        img: '../../assets/c.png',
        text: '自体脂肪',
        url: '../project-list/project-list?did=37'
      },
      {
        img: '../../assets/g.png',
        text: '私密',
        url: '../project-list/project-list?did=35'
      },
      {
        img: '../../assets/i.png',
        text: '美体塑形',
        url: '../project-list/project-list?did=38'
      },
      {
        img: '../../assets/b.png',
        text: '植发',
        url: '../project-list/project-list?did=33'
      }
    ],
    showAd: true,
    newConfig: {
      autoplay: true,
      interval: 4000,
      duration: 1000
    },
    currentBannerIndex: 0,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    news: [],
    doctorList: [],
    exampleList: [],
    searchList: [
      {
        img: "http://wx.mtmstar.com/Upload/Album/2018/11/27/1d60d641-364f-41be-806b-15df34f313e3.png",
        text: "美妆",
        url: '../product-list/product-list'
      },
      {
        img: "http://wx.mtmstar.com/Upload/Album/2018/11/27/5532b92f-6809-4c32-a419-12bdaaf7a702.png",
        text: "医生",
        url: '../doctor-list/doctor-list'
      },
      {
        img: "http://wx.mtmstar.com/Upload/Album/2018/11/27/8802f1f5-a010-4575-a182-6a05cb70e7c1.png",
        text: "案例",
        url: '../example-list/example-list'
      }
    ],
    instiList: [],
    imgUrls: [
      {
      link: '/pages/index/index',
      url: 'http://runway.goldenhealthcare.cn/FileUpload/Album/2018/7/4/df9690d7-43e7-4716-a919-d66dff879057.png'
    }, {
      link: '/pages/logs/logs',
      url: 'http://runway.goldenhealthcare.cn/FileUpload/Album/2018/7/4/df9690d7-43e7-4716-a919-d66dff879057.png'
    }, {
      link: '/pages/index/index',
      url: 'http://runway.goldenhealthcare.cn/FileUpload/Album/2018/7/4/df9690d7-43e7-4716-a919-d66dff879057.png'
    }],
    consultActive: false
  },
  onLoad: function() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this._initData()
  },
  getMoreConsult() {
    this.setData({
      consultActive: !this.data.consultActive
    })
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: '400-878-6606' //仅为示例，并非真实的电话号码
    })
  },
  onItemClick(e) {
    console.log(e)
    wx.navigateTo({
      url: `../doctor-detail/doctor-detail?mid=${e.currentTarget.dataset.item.mid}`
    })
  },
  instiItemClick(e) {
    console.log(e)
    wx.navigateTo({
      url: `../hospital-detail/hospital-detail?mid=${e.currentTarget.dataset.item.HosID}`
    })
  },
  _initData() {
    mainInList({
      data: {
        index: 1,
        size: 4,
        categorys: 143,
        menuID: 5,
      },
      success: res => {
        if (res.state) {
          let banners = []
          res.data.list.forEach((item) => {
            banners.push({
              link: item.url !== null ? item.url : "",
              url: item.images.length > 0 ? item.images[0].URL : ""
            })
          })
          this.setData({
            imgUrls: banners
          })
        }
      }
    })
    mainInList({
      data: {
        index: 1,
        size: 5,
        categorys: 147,
        menuID: 7,
      },
      success: res => {
        if (res.state) {
          let news = []
          res.data.list.forEach((item) => {
            news.push({
              id: item.id,
              text: item.title,
              time: item.publishDate.split('/').slice(1, 3).join('/')
            })
          })
          this.setData({
            news: news
          })
        }
      }
    })
    this._getEvents()
    this._getAd()
    this._getDoctor()
    this._getHospital()
    this._getSuccess()
  },
  _getEvents() {
    categoryByLevel({
      data: {
        menuID: 30,
        id: 171,
        level: 2
      },
      success: res => {
        if (res.state) {
          this.setData({
            events: res.data
          })
        }
      }
    })
  },
  _getAd() {
    mainInList({
      data: {
        index: 1,
        size: 1,
        categorys: 158,
        menuID: 7
      },
      success: res => {
        if (res.state) {
          this.setData({
            adBox: res.data.list[0]
          })
        }
      }
    })
  },
  bannerShift(e) {
    this.setData({
      currentBannerIndex: e.detail.current
    })
  },
  _getDoctor() {
    doctorList({
      data: {
        isTop: true,
        HosID: 0,
        index: 1,
        size: 10
      },
      success: res => {
        if (res.state) {
          res.data.map((item) => {
            item.tag = item.Tags.slice(0, -1).split('|').join(' ')
            return item
          })
          this.setData({
            doctorList: res.data
          })
        }
      }
    })
  },
  _getHospital() {
    hospitalList({
      data: {
        isTop: true,
        index: 1,
        size: 10,
        lat: '32.010413',
        lng: '118.737097'
      },
      success: res => {
        if (res.state) {
          // res.data.map((item) => {
          //   item.tag = item.Tags.slice(0, item.Tags.length - 1)
          //   item.Distance = item.Distance > 10000 ? '>10km' : (item.Distance / 1000).toFixed(2)
          //   return item
          // })
          this.setData({
            instiList: res.data
          })
        }
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  _getSuccess(condition) {
    sucessCaseList({
      data: {
        index: 1,
        size: 20,
        types: 0,
        mid: 0
      },
      success: res => {
        wx.hideLoading()
        if (res.state) {
          if (res.data.list.length > 0) {
            let list = []
            res.data.list.forEach((item) => {
              list.push({
                mid: item.mid,
                beforeImg: item.skulist[0].img,
                afterImg: item.skulist[item.skulist.length - 1].img,
                name: item.PatientName,
                doctor: item.DocName,
                bSaying: item.skulist[0].Describe,
                aSaying: item.skulist[item.skulist.length - 1].Describe,
                surgery: item.Operation.split('，')
              })
            })
            this.setData({
              exampleList: this.data.exampleList.concat(list)
            })
          } else {
            this.setData({
              noData: true
            })
          }

        }
      }
    })
  },
  closeAd() {
    this.setData({
      showAd: false
    })
  }
})